using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
using HotFixFramework;
using LitJson;

namespace ClientExtensions
{
    public class CodeGenerator
    {
        public static void GeneratorResVariable()
        {
            string CodePath = "Asset/InHotFixDLL/ResVariable.cs";
            string HotFixNamespace = "KBE_HotFix";
            string ResVariableClassName = "ResVariable";
            string relatePath = "Assets/" + HotFixConfigs.BundleRootDir;

            string variableNameSpace = "namespace " + HotFixNamespace + "\n";
            string ResVariableClass = "    public static class " + ResVariableClassName + "\n";
            string bodyItem = "        public const string {0} = {1};\n";

            string bundlePath = FileHelper.CombinePath(FileHelper.EditorProjectDir, relatePath);
            List<string> paths = new List<string>();
            List<string> files = new List<string>();
            FileHelper.Recursive(bundlePath, files, paths);
            string body = "";
            foreach (var file in files)
            {
                var fileName = file.Substring(file.LastIndexOf(relatePath) + relatePath.Length + 1);
                var variable = fileName.Replace(" ", "");
                variable = variable.Replace("/", "_");
                variable = variable.Replace("(", "_");
                variable = variable.Replace(")", "");
                variable = variable.Replace(".", "_");
                fileName = "\"" + fileName + "\"";
                body = body + string.Format(bodyItem, variable, fileName);
            }

            ResVariableClass = ResVariableClass + "    {\n" + body + "\n    }";
            variableNameSpace = variableNameSpace + "{\n" + ResVariableClass + "\n}";
            FileHelper.FileWriteAllText(FileHelper.CombinePath(FileHelper.EditorProjectDir, CodePath), variableNameSpace);
        }
    }
}